class ArticlesController < ApplicationController

  layout 'admin'
  before_filter :check_login_status
  
  def index
    redirect_to :action => "list"    
  end
  
  def list
    @article_pages, @articles = paginate :articles,
                                         :order => 'created_at DESC',
                                         :per_page => 10
  end
  
  def show
    @article = Article.find(params[:id])
  end
  
  def new
    @article = Article.new
    @categories = Category.find(:all)
  end
  
  def create
    @article = Article.new(params[:article])
    @article.user = User.find(session[:user_id])
    
    if @article.save
      flash[:notice] = '새로운 글이 추가되었습니다.'
      redirect_to :action => 'show', :id => @article
    else
      @categories = Category.find(:all)
      render :action => 'new'
    end
  end
  
  def edit
    @article = Article.find(params[:id])
    @categories = Category.find(:all)
  end
  
  def update
    @article = Article.find(params[:id])
    
    if @article.update_attributes(params[:article])
      flash[:notice] = '글이 수정되었습니다.'
      redirect_to :action => 'show', :id => @article
    else
      @categories = Category.find(:all)
      render :action => 'edit'
    end
  end
  
  def destroy
    @article = Article.find(params[:id])
    @article.destory
    redirect_to :action => 'list'
  end
  
end
